from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

class RestfulAPI(APIView):
    def get(self, request):
        # 处理GET请求示例
        return Response({
            'status': 'success',
            'message': 'GET请求成功'
        }, status=status.HTTP_200_OK)

    def post(self, request):
        try:
            data = request.data  # DRF自动解析JSON数据
            print(data)
            return Response({
                'status': 'success',
                'data': data
            }, status=status.HTTP_201_CREATED)
        except Exception as e:
            return Response({
                'status': 'error',
                'message': str(e)
            }, status=status.HTTP_400_BAD_REQUEST)
    
    def put(self, request):
        try:
            data = request.data  # DRF自动解析JSON数据
            print(data)
            return Response({
                'status': 'success',
                'data': data
            }, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                'status': 'error',
                'message': str(e)
            }, status=status.HTTP_400_BAD_REQUEST)
        
    def delete(self, request):
        try:
            data = request.data  # DRF自动解析JSON数据
            print(data)
            return Response({
                'status': 'success',
                'data': data
            }, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                'status': 'error',
                'message': str(e)
            }, status=status.HTTP_400_BAD_REQUEST)
    